#!/bin/sh
#
# Copyright The OpenZipkin Authors
# SPDX-License-Identifier: Apache-2.0
#

# ENTRYPOINT script that starts Cassandra
#
# This intentionally locates config using the current working directory, in order to consolidate
# Dockerfile instructions to WORKDIR
set -eu

# Apply one-time deferred configuration that relies on ENV variables
#
# If the schema has been removed due to mounting, restore from our backup. see: install
if [ ! -d data/zipkin2 ]; then
  cp -rf data-backup/* data/
fi

IP="$(hostname -i || echo '127.0.0.1')"
sed -i "s/127.0.0.1/${IP}/g" conf/cassandra.yaml

# Replace the logging level
sed -i "s/log4j.rootLogger.*/log4j.rootLogger=${LOGGING_LEVEL}, stdout/" conf/log4j.properties

# Use agent to allow instrumentation of a lambda: CASSANDRA-16304
JAMM_JAR=$(ls lib/jamm-*.jar)

# Configure the Docker HEALTHCHECK
export HEALTHCHECK_IP=${IP}
export HEALTHCHECK_PORT=9042
export HEALTHCHECK_KIND=tcp

echo Starting Cassandra
# -cp 'classes:lib/*' allows layers to patch the image without packaging or
# overwriting jars.
#
# We also add exports and opens from both Cassandra v4 and v5, except for
# attach, compiler and rmi because our JRE excludes these modules.
#
# Merging makes adding Cassandra v5 easier and lets us share a common JRE 17+
# with other test images even if Cassandra v4 will never officially support it.
# https://github.com/apache/cassandra/blob/cassandra-4.0.11/conf/jvm11-server.options
# https://github.com/apache/cassandra/blob/cassandra-5.0/conf/jvm17-server.options
#
# Finally, we allow security manager to prevent JRE 21 crashing when Cassandra
# attempts ThreadAwareSecurityManager.install()
exec java -cp 'classes:lib/*' ${JAVA_OPTS} \
  -Djava.security.manager=allow \
  -Xbootclasspath/a:${JAMM_JAR} -javaagent:${JAMM_JAR} \
  -Djdk.attach.allowAttachSelf=true \
  --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \
  --add-exports java.base/jdk.internal.ref=ALL-UNNAMED \
  --add-exports java.base/sun.nio.ch=ALL-UNNAMED \
  --add-exports java.sql/java.sql=ALL-UNNAMED \
  --add-exports java.base/java.lang.ref=ALL-UNNAMED \
  --add-exports jdk.unsupported/sun.misc=ALL-UNNAMED \
  --add-opens java.base/java.lang.module=ALL-UNNAMED \
  --add-opens java.base/jdk.internal.loader=ALL-UNNAMED \
  --add-opens java.base/jdk.internal.ref=ALL-UNNAMED \
  --add-opens java.base/jdk.internal.reflect=ALL-UNNAMED \
  --add-opens java.base/jdk.internal.math=ALL-UNNAMED \
  --add-opens java.base/jdk.internal.module=ALL-UNNAMED \
  --add-opens java.base/jdk.internal.util.jar=ALL-UNNAMED \
  --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED \
  --add-opens java.base/java.io=ALL-UNNAMED \
  --add-opens java.base/java.nio=ALL-UNNAMED \
  --add-opens java.base/sun.nio.ch=ALL-UNNAMED \
  --add-opens java.base/java.io=ALL-UNNAMED \
  --add-opens java.base/java.lang.reflect=ALL-UNNAMED \
  --add-opens java.base/java.lang=ALL-UNNAMED \
  --add-opens java.base/java.util=ALL-UNNAMED \
  --add-opens java.base/java.nio=ALL-UNNAMED \
  --add-opens java.base/java.util.concurrent=ALL-UNNAMED \
  --add-opens java.base/java.util.concurrent.atomic=ALL-UNNAMED \
  -Djava.io.tmpdir=/tmp \
  -Dcassandra-foreground=yes \
  -Dcassandra.storagedir=${PWD} \
  -Dcassandra.triggers_dir=${PWD}/triggers \
  -Dcassandra.config=file:${PWD}/conf/cassandra.yaml \
  -Dlog4j.configuration=file:${PWD}/conf/log4j.properties \
  org.apache.cassandra.service.CassandraDaemon "$@"
